package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.SysFeedbackInfoVo;
import com.ruoyi.system.mapper.SysProjectMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysFeedbackInfoMapper;
import com.ruoyi.system.domain.SysFeedbackInfo;
import com.ruoyi.system.service.ISysFeedbackInfoService;

/**
 * 反馈信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-01
 */
@Service
public class SysFeedbackInfoServiceImpl implements ISysFeedbackInfoService 
{
    @Autowired
    private SysFeedbackInfoMapper sysFeedbackInfoMapper;

    @Autowired
    private SysProjectMapper sysProjectMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询反馈信息
     * 
     * @param feedbackId 反馈信息主键
     * @return 反馈信息
     */
    @Override
    public SysFeedbackInfo selectSysFeedbackInfoByFeedbackId(Long feedbackId)
    {
        return sysFeedbackInfoMapper.selectSysFeedbackInfoByFeedbackId(feedbackId);
    }

    /**
     * 查询反馈信息列表
     * 
     * @param sysFeedbackInfo 反馈信息
     * @return 反馈信息
     */
    @Override
    public List<SysFeedbackInfoVo> selectSysFeedbackInfoList(SysFeedbackInfo sysFeedbackInfo)
    {

        List<SysFeedbackInfoVo> result = new ArrayList<>();

        List<SysFeedbackInfo> sysFeedbackInfos = sysFeedbackInfoMapper.selectSysFeedbackInfoList(sysFeedbackInfo);

        for (SysFeedbackInfo feedbackInfo : sysFeedbackInfos) {

            SysFeedbackInfoVo vo = new SysFeedbackInfoVo();
            BeanUtils.copyProperties(feedbackInfo,vo);

            if (feedbackInfo.getProjectId()!=null){
                SysProject sysProject = sysProjectMapper.selectSysProjectByProjectId(feedbackInfo.getProjectId());

                if (sysProject!=null){
                    vo.setProjectName(sysProject.getProjectName());
                }

            }

            if (feedbackInfo.getSubmitUserId()!=null){

                SysUser sysUser = sysUserMapper.selectUserById(feedbackInfo.getSubmitUserId());

                if (sysUser!=null){
                    vo.setSubmitUserName(sysUser.getNickName());
                }


            }

            result.add(vo);
        }


        return result;
    }

    /**
     * 新增反馈信息
     * 
     * @param sysFeedbackInfo 反馈信息
     * @return 结果
     */
    @Override
    public int insertSysFeedbackInfo(SysFeedbackInfo sysFeedbackInfo)
    {
        sysFeedbackInfo.setCreateTime(DateUtils.getNowDate());
        sysFeedbackInfo.setSubmitUserId(SecurityUtils.getUserId());
        return sysFeedbackInfoMapper.insertSysFeedbackInfo(sysFeedbackInfo);
    }

    /**
     * 修改反馈信息
     * 
     * @param sysFeedbackInfo 反馈信息
     * @return 结果
     */
    @Override
    public int updateSysFeedbackInfo(SysFeedbackInfo sysFeedbackInfo)
    {
        sysFeedbackInfo.setUpdateTime(DateUtils.getNowDate());
        return sysFeedbackInfoMapper.updateSysFeedbackInfo(sysFeedbackInfo);
    }

    /**
     * 批量删除反馈信息
     * 
     * @param feedbackIds 需要删除的反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteSysFeedbackInfoByFeedbackIds(Long[] feedbackIds)
    {
        return sysFeedbackInfoMapper.deleteSysFeedbackInfoByFeedbackIds(feedbackIds);
    }

    /**
     * 删除反馈信息信息
     * 
     * @param feedbackId 反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteSysFeedbackInfoByFeedbackId(Long feedbackId)
    {
        return sysFeedbackInfoMapper.deleteSysFeedbackInfoByFeedbackId(feedbackId);
    }


    @Override
    public AjaxResult appFeedBackList() {
        List<SysFeedbackInfoVo> result = new ArrayList<>();

        List<SysFeedbackInfo> list = sysFeedbackInfoMapper.selectSysFeedbackInfoList(null);

        for (SysFeedbackInfo feedbackInfo : list) {

            SysFeedbackInfoVo vo = new SysFeedbackInfoVo();
            BeanUtils.copyProperties(feedbackInfo,vo);

            if (feedbackInfo.getProjectId()!=null){
                SysProject sysProject = sysProjectMapper.selectSysProjectByProjectId(feedbackInfo.getProjectId());

                if (sysProject!=null){
                    vo.setProjectName(sysProject.getProjectName());
                }

            }

            if (feedbackInfo.getSubmitUserId()!=null){

                SysUser sysUser = sysUserMapper.selectUserById(feedbackInfo.getSubmitUserId());

                if (sysUser!=null){
                    vo.setSubmitUserName(sysUser.getNickName());
                }

            }


            if (StrUtil.isNotBlank(feedbackInfo.getImageUrls())){

                vo.setFileList(Arrays.asList(feedbackInfo.getImageUrls().split(",")));

            }


            result.add(vo);
        }

        return AjaxResult.success(result);
    }
}
